package com.trkj.base;

import android.view.View;

import com.trkj.widget.refresh.SwipeRefreshLayout;

/**
 * Created by LiZanhong on 2015/06/15.
 */
public abstract class BaseRefreshFragment extends BaseFragment
        implements SwipeRefreshLayout.OnRefreshListener,
        SwipeRefreshLayout.OnLoadListener{

    protected SwipeRefreshLayout refreshLayout;
    private SwipeRefreshLayout.Mode mode;

    public void setSwiftLayout(View root, int swiftLayoutId) {
        this.refreshLayout = (SwipeRefreshLayout)root.findViewById(swiftLayoutId);

        this.mode = getMode();
        refreshLayout.setMode(this.mode);

        bindSwiftLayoutListener();
        refreshLayout.setColor(android.R.color.holo_green_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_blue_light);
        refreshLayout.setLoadNoFull(true);
    }

    /**
     * 绑定刷新事件
     */
    private void bindSwiftLayoutListener() {
        if(mode == SwipeRefreshLayout.Mode.PULL_FROM_START) {
            refreshLayout.setOnRefreshListener(this);
        }else if(mode == SwipeRefreshLayout.Mode.PULL_FROM_END){
            refreshLayout.setOnLoadListener(this);
        }else if(mode == SwipeRefreshLayout.Mode.BOTH){
            refreshLayout.setOnRefreshListener(this);
            refreshLayout.setOnLoadListener(this);
        }
    }

    protected SwipeRefreshLayout.Mode getMode(){
        return SwipeRefreshLayout.Mode.PULL_FROM_START;
    };


    /**
     * 能过同步方式获取数据
     * @return 返回获取的数据
     */
    protected String obtainJSON(){
        return null;
    }


    @Override
    public void onLoad() {
        refreshLayout.setLoading(false);
    }

    @Override
    public void onRefresh() {
        refreshLayout.setRefreshing(false);
    }

    protected SwipeRefreshLayout getRefreshLayout(){
        return refreshLayout;
    }
}
